package org.example.day10;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 4.1.3.2: 使用Condition类实现等待/通知
 *      执行condition.await() 前加锁.
 */
public class MyThread8 {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    public void waitMethod(){
        try {
            lock.lock();
            System.out.println("A");
            condition.await();
            System.out.println("B");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("锁释放了!");
        }
    }

    public static void main(String[] args) {
        MyThread8 m8 = new MyThread8();
        Thread8A ta = new Thread8A(m8);
        ta.start();
    }
}

class Thread8A extends Thread{
    private MyThread8 m8;

    public Thread8A(MyThread8 m8) {
        this.m8 = m8;
    }

    @Override
    public void run() {
        m8.waitMethod();
    }
}